# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from odoo.exceptions import ValidationError, Warning
from ..models.models import WORK_ORDER_STATE

class ErBindLiftWizard(models.TransientModel):
    """
    救援单位绑定电梯
    """
    _name = 'tyibs.er.bind.lift.wizard'

    salvager_company_id = fields.Many2one("tyibs.base.maintenance.company", domain="[('is_salvager_unit','=',True)]", required=True, string=u"救援单位")
    salvager_site_id = fields.Many2one("tyibs.base.maintenance.company.site", domain="[('enterprise_id','=',salvager_company_id)]", required=True, string=u"救援站点")
    # 要绑定的电梯
    lift_ids = fields.Many2many("tyibs.base.lift","bind_rel_lift", "bind_id", "lift_id", string=u"电梯", domain="[('salvager_unit_id','=',None)]")

    @api.multi
    def start_bind(self,values):
        """
        正式绑定电梯
        """
        self.ensure_one()
        self.lift_ids.write({'salvager_unit_id':self.salvager_company_id.id, 'salvager_site_id': self.salvager_site_id.id })
        return { 
            'type': 'ir.actions.act_window', 
            'res_model': 'tyibs.base.lift', 
            'view_type': 'form', 
            'view_mode': 'tree,form', 
            'name':'本次绑定的电梯',
            'domain':"[('id', 'in',%s)]" %([item.id for item in self.lift_ids]),
            'target': 'current', 
        }


class AddCommentWizard(models.TransientModel):
    """
    增加跟踪记录
    """
    _name = 'tyibs.er.work.order.comment.wizard'

    contact = fields.Char(string=u"联系人", size=100, index=True)
    phone = fields.Char(string=u"联系电话", size=50, index=True)
    stage = fields.Selection(WORK_ORDER_STATE, string=u"隶属阶段", required=True, default="draft")
    do_time = fields.Datetime(string=u"处理时间", required=True, default= lambda self:fields.Datetime.now())
    operation_category_id = fields.Many2one("tyibs.er.work.order.operation.category", required=True, string=u"活动类型")
    content = fields.Text(string=u"进展情况")
    record_attachment_id = fields.Binary(string=u"录音记录", attachment=True)
    res_model = fields.Char('关联的模型', index=True)
    res_id = fields.Integer('关联的记录', index=True)
    remark = fields.Text(string=u"备注")

    @api.multi
    def add_comment(self):
        """
        添加跟踪记录
        """
        self.ensure_one()
        self.env['tyibs.er.work.order.comment'].create({
            'contact': self.contact,
            'phone': self.phone,
            'stage': self.stage,
            'do_time': self.do_time,
            'operation_category_id': self.operation_category_id.id,
            'content': self.content,
            'res_model': self.res_model,
            'res_id': self.res_id,
            'remark': self.remark
        })
        return { 
            'type': 'ir.actions.act_window', 
            'res_model': self.res_model,
            'res_id': self.res_id, 
            'view_type': 'form', 
            'view_mode': 'form', 
            'target': 'current', 
        }


class CancelOrderWizard(models.TransientModel):
    """
    取消接警
    """
    _name = 'tyibs.er.work.order.cancel.wizard'

    save_result = fields.Selection([('get_out_yourself', '自行脱困'),('misinformation', '误报')], string=u"取消原因", required=True)
    remark = fields.Text(string=u"备注", required=True)

    @api.multi
    def cancel_order(self, values):
        self.ensure_one()
        res_model = values.get("active_model")
        res_id = values.get("active_id")
        order = self.env[res_model].browse(res_id)
        order.cancel_order({
            'save_result': self.save_result,
            'remark': self.remark
        })